package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.mgr.RegisterPlayerNumMgr;
import com.yanqu.road.web.mgr.RoleCreateCountMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(urlPatterns = "/consoleQueryRoleCreate")
public class ConsoleQueryRoleCreate extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(ConsoleQueryRoleCreate.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request consoleQueryRoleCreate");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		int channelId = 0;
		int type = 1; //1 创角  2 有效创角   3 注册人数
		if(paramObj.containsKey("channelId")){
			channelId = paramObj.getIntValue("channelId");
		}
		if(paramObj.containsKey("type")){
			type = paramObj.getIntValue("type");
		}

		JSONObject resultObj = new JSONObject();
		List<ServerInfo> serverList = ServerListMgr.getSplitFlowServerList();
		for (ServerInfo serverInfo : serverList) {
			if(channelId > 0 && serverInfo.getChannelId() != channelId){
				continue;
			}else if(type == 1 && RoleCreateCountMgr.getRoleCreateCountByServerId(serverInfo.getServerId()) >= serverInfo.getCreateRoleCountLimit()){//超过分流
				continue;
			}else if(type == 2 && RoleCreateCountMgr.getValidCreateCountByServerId(serverInfo.getServerId()) >= serverInfo.getCreateRoleCountLimit()){//超过分流
				continue;
			}else if(type == 3 && RegisterPlayerNumMgr.getRegisterNumByServerId(serverInfo.getServerId()) >= serverInfo.getCreateRoleCountLimit()){//超过分流
				continue;
			}
			String serverData = "";
			try {
				serverData = HttpHelper.doPostJsonTimeOut(serverInfo.getWebServerUrl() + "/queryServerRoleCreate", paramObj.toJSONString(), "UTF-8", new HashMap<>());
				JSONObject jsonObj = JSONObject.parseObject(serverData);
				if(null != jsonObj) {
					JSONObject dataObj = jsonObj.getJSONObject("" + serverInfo.getServerId());
					if (null != dataObj) {
						resultObj.put("" + serverInfo.getServerId(), dataObj);
					}else {
						if(jsonObj.containsKey("state")){
							logger.error("state code : {}", jsonObj.get("state"));
							//直接返回缓存数据
							dataObj = new JSONObject();
							if(type == 1){
								dataObj.put("role", RoleCreateCountMgr.getRoleCreateCountByServerId(serverInfo.getServerId()));
							}else if(type == 2){
								dataObj.put("role", RoleCreateCountMgr.getValidCreateCountByServerId(serverInfo.getServerId()));
							}else if(type == 3){
								dataObj.put("role", RegisterPlayerNumMgr.getRegisterNumByServerId(serverInfo.getServerId()));
							}
							dataObj.put("limit", serverInfo.getCreateRoleCountLimit());
							resultObj.put("" + serverInfo.getServerId(), dataObj);
						}
					}
				}
			} catch (Exception ex) {
				logger.error("json parse error,{},error:", serverData, ex);
			}

		}
		sendMessage(response, resultObj.toJSONString());
	}

}
